﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using Lbb.Localization;
using Lbb.URating.DL;
using Lbb.URating.Localization;
using Lbb.Data;

namespace HU8.UCenter.Plugins.URating.DAL
{
    /// <summary>
    /// 点击器操作库
    /// </summary>
    /// <author>Chiron</author>
    public class U_ClickRepository : RepositoryBase<U_Click>
    {
        #region Ctors
        /// <summary>
        /// 构造点击器操作库
        /// </summary>
        /// <param name="db"></param>
        public U_ClickRepository(URatingDataContext db)
            : base(db)
        {
        }
        #endregion Ctors

        #region Get
        /// <summary>
        /// 取得指定点击器
        /// </summary>
        /// <param name="id">点击器ID</param>
        /// <returns></returns>
        public U_Click Get(Guid? id)
        {
            return id.HasValue && id != Guid.Empty ? Get(d => d.ClickID == id) : null;
        }
        #endregion Get

        #region Find
        /// <summary>
        /// 搜索点击器
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <param name="isRating">是否为评分项</param>
        /// <param name="minRating">最小分值</param>
        /// <param name="maxRating">最大分值</param>
        /// <param name="categoryIds">所属分类</param>
        /// <returns></returns>
        public IQueryable<U_Click> Find(string keyword = null, bool? isRating = null, decimal? minRating = null, decimal? maxRating = null, IEnumerable<Guid> categoryIds = null)
        {
            var data = GetAll();

            if (!string.IsNullOrEmpty(keyword)) data = data.Where(d => d.Title.Contains(keyword));

            if (isRating.HasValue) data = data.Where(d => d.IsRating == isRating.Value);

            if (minRating.HasValue) data = data.Where(d => d.MinRating >= minRating.Value);

            if (maxRating.HasValue) data = data.Where(d => d.MaxRating <= maxRating.Value);

            if (categoryIds != null && (categoryIds = categoryIds.Where(d => d != Guid.Empty).ToList()).Count() > 0) data = data.Where(d => categoryIds.Contains(d.CategoryID));

            return data;
        }
        #endregion Find

        #region Override
        /// <summary>
        /// 添加点击器
        /// </summary>
        /// <param name="obj">点击器</param>
        public override void Add(U_Click obj)
        {
            if (obj != null && obj.ClickID == Guid.Empty) obj.ClickID = Guid.NewGuid();
            base.Add(obj);
        }
        /// <summary>
        /// 更新点击器
        /// </summary>
        /// <param name="obj">点击器</param>
        public override void Update(U_Click obj)
        {
            CheckArg(obj);
            var old = Get(obj.ClickID);
            if (old == null) throw new ArgumentNullException(string.Format(OperatorRes.NotFindEditingObj, U_ClickRes.ClassName));

            try
            {
                Table.Attach(obj, old);
            }
            catch (InvalidOperationException)
            {
                DB.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
            }
        }
        #endregion Override

    }
}